package im.starchat.chat.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;

import im.starchat.chat.R;
import im.starchat.chat.adapter.ClassCenterTabTypeAdapter;
import im.starchat.chat.adapter.SmartFragmentStatePagerAdapter;
import im.starchat.chat.view.HorizontalListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class LiveContainerFragment extends Fragment implements OnClickListener {

    @InjectView(R.id.live_viewpager)
    ViewPager mLiveViewPager;
    @InjectView(R.id.horizon_listview)
    HorizontalListView mHorizontalTabListView;


    private ClassCenterTabTypeAdapter mClassTabAdapter;

    LiveFragment mAttentionFragment;//关注
    LiveFragment mPopularityFragment;//热门
    LiveFragment mNewestFragment;//最新

    LivePagerAdapter mLiveAdapter;
    List<Fragment> liveFragmentList;
    private ArrayList<String> mTabs = new ArrayList<>();
    MainChannelPagerOnPageChangeListener mMainChannelPagerOnPageChangeListener;
    private int mCurrentTab = 0;



    public static LiveContainerFragment newInstance() {
        return new LiveContainerFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        liveFragmentList = new ArrayList<>();
        mAttentionFragment = LiveFragment.newInstance("following");
        liveFragmentList.add(mAttentionFragment);
        mPopularityFragment = LiveFragment.newInstance("hot");
        liveFragmentList.add(mPopularityFragment);
        mNewestFragment = LiveFragment.newInstance("newest");
        liveFragmentList.add(mNewestFragment);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_live_container, container, false);
        ButterKnife.inject(this, view);

        mLiveAdapter = new LivePagerAdapter(getChildFragmentManager(), liveFragmentList);
        mLiveViewPager.setAdapter(mLiveAdapter);

        mLiveViewPager.setOffscreenPageLimit(liveFragmentList.size());
        mMainChannelPagerOnPageChangeListener = new MainChannelPagerOnPageChangeListener();
        mLiveViewPager.setOnPageChangeListener(mMainChannelPagerOnPageChangeListener);
        mLiveViewPager.setCurrentItem(0);

        mTabs.add("关注");
        mTabs.add("热门");
        mTabs.add("最新");
        mClassTabAdapter = new ClassCenterTabTypeAdapter(
                getActivity(),mTabs);
        mHorizontalTabListView
                .setAdapter(mClassTabAdapter);
        mHorizontalTabListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                mClassTabAdapter.setSelectIndex(arg2);
                mClassTabAdapter.notifyDataSetChanged();
                mLiveViewPager.setCurrentItem(arg2);
                mCurrentTab = arg2;
            }
        });
        return view;
    }

    public class MainChannelPagerOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
//            favTypeHost.check(position == 0 ? R.id.fav_topic : R.id.fav_comic);
            //TODO 主界面接口回调
            mClassTabAdapter.setSelectIndex(position);
            mClassTabAdapter.notifyDataSetChanged();
            mCurrentTab = position;

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    public class LivePagerAdapter extends SmartFragmentStatePagerAdapter {
        List<Fragment> fragmentList;

        public LivePagerAdapter(android.support.v4.app.FragmentManager fragmentManager, List<Fragment> fragments) {
            super(fragmentManager);
            this.fragmentList = fragments;
        }

        @Override
        public Fragment getItem(int position) {

            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.channel_popularity:
//                if(mLiveViewPager != null){
//                    mLiveViewPager.setCurrentItem(0);
//                }
//                break;
//            case R.id.channel_newest:
//                if(mLiveViewPager != null){
//                    mLiveViewPager.setCurrentItem(1);
//                }
//                break;
        }
    }

}
